<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="题型" prop="questionType">
           <el-select v-model="queryParams.questionType" placeholder="题型" clearable>
            <el-option
                v-for="item in this.questionTypelist"
                :key="item.key" 
                :value="item.key" 
                :label="item.value"
              ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题干：">
        <el-input v-model="queryParams.title" clearable></el-input>
      </el-form-item>

      <el-form-item>
        
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-popover placement="bottom" trigger="click">
          <el-button type="warning" size="mini" v-for="item in editUrlEnum" :key="item.key"
                     @click="$router.push({path:item.value})">{{item.name}}
          </el-button>
          <el-button slot="reference" type="primary" class="link-left" icon="el-icon-plus" size="mini">添加</el-button>
        </el-popover>
        <router-link :to="{path:'/exam/question/edit/pinsert'}" class="link-left">
          <el-button type="primary" icon="el-icon-plus" size="mini">批量添加</el-button>
        </router-link>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"  icon="el-icon-download" @click="exportExcel" size="mini">下载模板</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="roleList">
     
      <el-table-column prop="id" label="序号" width="100px"/>
      <el-table-column prop="questionType" label="题型" :formatter="questionTypeFormatter" width="140px"/>
      <el-table-column prop="shortTitle" label="题干" show-overflow-tooltip/>
      <el-table-column prop="score" label="分数" width="80px"/>
      <el-table-column prop="difficult" label="难度" width="80px"/>
      <el-table-column prop="createTime" label="创建时间" width="200px"/>
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column width="360px" label="操作" align="center">
        <template slot-scope="{row}">
          <el-button size="mini"  @click="editQuestion(row)">编辑</el-button>
          <!-- <router-link :to="{path:'/category/lecturetitle/edit', query:{id:row.id}}" class="link-left">
            <el-button size="mini" >编辑</el-button>
          </router-link> -->
          <el-button  size="mini" type="danger" @click="handleDelete(row)" class="link-left">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParam.pageNum"
      :limit.sync="queryParam.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->

    <!-- 分配角色数据权限对话框 -->
 
  </div>
</template>

<script>
import { pageList,changeStatus,deleteQuestion,upload} from "@/api/xzs/question";
import Pagination from '@/components/Pagination'
import axios from 'axios'
import request from '@/utils/request'
export default {
  components: { Pagination },
  data() { 
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 数据范围选项
    
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      questionTypelist:[
          { key: 1, value: '单选题'},
          {key: 2, value: '多选题'},
          {key: 3, value: '判断题'},
          {key: 5, value: '简答题'}
      ],
      editUrlEnum:[{ key: 1, value: '/exam/question/edit/singleChoice', name: '单选题' },
        { key: 2, value: '/exam/question/edit/multipleChoice', name: '多选题' },
        { key: 3, value: '/exam/question/edit/trueFalse', name: '判断题' },
       
        { key: 5, value: '/exam/question/edit/shortAnswer', name: '简答题' }
        ],
      // 查询参数
      queryParams: {
        id: null,
        questionType: null,
        title:null,
        level:null,
        subjectId:null,
        pageIndex: 1,
        pageSize: 10
      },
      queryParam: {
        pageNum: 1,    
        pageSize: 10 
      },
      questionShow: {
        qType: 0,
        dialog: false,
        question: null,
        loading: false
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    
  },
  methods: {
    editQuestion (row) {
      if(row.questionType==1)
      {
        let url ='/exam/question/edit/singleChoice/'
        this.$router.push({ path: url, query:{id:row.id}})
      }
      else if(row.questionType==2)
      {
        let url ='/exam/question/edit/multipleChoice/'
        this.$router.push({ path: url, query:{id:row.id}})
      }
       else if(row.questionType==3)
      {
        let url ='/exam/question/edit/trueFalse/'
        this.$router.push({ path: url, query:{id:row.id}})
      }
       else if(row.questionType==5)
      {
        let url ='/exam/question/edit/shortAnswer/'
        this.$router.push({ path: url, query:{id:row.id}})
      }
    },
    questionTypeFormatter : function(row, column) {
				return row.questionType == 1 ? "单选题" : row.questionType == 2 ? "多选题" : row.questionType == 3 ? "判断题": row.questionType == 5 ? "简答题": "暂无";
      },
      // 下载模板
    exportExcel () {
       request({
        method: "post",
        url: "/api/admin/excel/export_question_template", // 请求地址
        data: null, // 参数
        responseType: "blob", // 表明返回服务器返回的数据类型
        headers: { 
          "Content-Type": "application/json",
          //Authorization: 'Bearer ' + getToken()
        }
      }).then(res => { // 处理返回的文件流
    
        const blob = new Blob([res]);
        const fileName = "试题添加模板.xlsx";
        const elink = document.createElement("a");
        elink.download = fileName;
        elink.style.display = "none";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
      });
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(
        response => {
         // console.log(response.response.list)
          this.roleList = response.response.list;
          this.total = response.response.total;
          this.loading = false;
        }
      );
    },
    // 角色状态修改
    handleStatusChange(row) {
      console.log(row.status)
      let text = row.status === 0? "停用" : "启用";
      console.log(text)
      this.$confirm('确认要"' + text + '""' + row.shortTitle + '"吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeStatus(row.id);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === 1 ? 0 : 1;
        });
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
   
    /** 删除按钮操作 */
    handleDelete(row) {   
      console.log(row.id)
      deleteQuestion(row.id).then(re => {
        if (re.code === 1) {
         this.getList();
          this.$message.success(re.message)
        } else {
          this.$message.error(re.message)
        }
      })
    }
  }
};
</script>